#每个模型有自己的方法类，实际项目中只用负责修改此文件
import cv2
from PIL import Image, ImageDraw, ImageFont
import sys
CENTERNET_PATH = '/home/predict'
sys.path.insert(0, CENTERNET_PATH)

from yolov4 import init_yolov4,detect,load_class_names,plot_boxes_cv2
import numpy as np
import math
from datetime import datetime

from model_utils import *
from post_alarm import *
from post_media import *
from project_deploy import *

class ModelFun:
    def __init__(self,model_name,gpu_id,id_list,electron_fences,ffmpeg_types):
        self.ffmpeg_types=ffmpeg_types
        self.electron_fences=electron_fences
        self.model_fun=model_name
        size=eval(self.model_fun+'.size')
        self.batch=len(id_list)
        self.id_list=id_list
        self.image_size = (size, size)
        engine_path = '/home/models/yolov4/'+self.model_fun+'/'+str(self.batch)+'/'+str(size)+'/model.trt'
        host = {"hostname":'xs', "ipaddr":'192.168.43.9'}
        mac = 'b0fc362f25d5'
        self.engine,self.context,self.buffers,self.num_classes,self.namesfile=init_yolov4(engine_path,self.model_fun,host,mac)
        self.num_classes= eval(self.model_fun+'.num_classes')
        self.model_parameters=[]   #用来传递模型的动态参数
        model_parameter=[]     #每一路模型的动态参数
        alarmNumberlist=[]
    #    if self.model_fun=='cap' or self.model_fun=='gang' or self.model_fun=='electric' or self.model_fun=='fire':
        if True:
            model_parameter.append(datetime.now())   #初始化更新时间
            model_parameter.append(0)                #初始化更新分数
            model_parameter.append(0)                #初始化保存状态，0为不录制，1为正在录制
            model_parameter.append(0)                #上一帧的报警数
            model_parameter.append(alarmNumberlist)  #alarmNumberlist
            model_parameter.append('')               #mp4
            model_parameter.append('')               #mp4名字
        for i in range(1,len(id_list)+1):
            self.model_parameters.append(model_parameter) 


    def detect_frame_all(self,img_in_all):
        self.conf_thresh=eval(self.model_fun+'.conf_thresh')
        nms_thresh=eval(self.model_fun+'.nms_thresh')
        self.boxes = detect(self.context, self.buffers, img_in_all, self.image_size, self.num_classes,self.batch,self.conf_thresh,nms_thresh)
        self.class_names = load_class_names(self.namesfile)

    def detect_frame(self,frame,cam_num):
        frame_ori=frame
        frame,ret=plot_boxes_cv2_zh(frame, self.boxes[cam_num-1],self.conf_thresh, savename='', class_names=self.class_names) 
        if self.model_fun=='cap':
            frame=self.fun_type_cap(frame,cam_num,frame_ori)
        elif self.model_fun=='gang':
            frame=self.fun_type_gang(frame,cam_num,frame_ori)
        elif self.model_fun=='zhuanggang':
            frame=self.fun_type_zhuanggang(frame,cam_num,frame_ori)
        elif self.model_fun=='electric':
            frame=self.fun_type_electric(frame,cam_num,frame_ori)
        elif self.model_fun=='fire':
            frame=self.fun_type_fire(frame,cam_num,frame_ori)

           # frame=self.fun_type_test(frame,cam_num,frame_ori)
        return frame

    def detect_frame_pic(self,frame,cam_num):
        frame_ori=frame
        frame,ret=plot_boxes_cv2_zh(frame, self.boxes[cam_num-1],self.conf_thresh, savename='', class_names=self.class_names) 
        return frame,ret


    def fun_type_test(self,frame,cam_num,frame_ori):  #用于测试功能，录制10秒的视频
        #动态参数传递进来
        save_time=self.model_parameters[cam_num-1][0]
        save_score=self.model_parameters[cam_num-1][1]
        save_state=self.model_parameters[cam_num-1][2]
        alarm_num=self.model_parameters[cam_num-1][3]
        alarmNumberlist=self.model_parameters[cam_num-1][4]
        mp4_name=self.model_parameters[cam_num-1][5]
     #   print(mp4_name)

        if mp4_name=='':
            mp4_name=self.save_mp4_init(save_time,cam_num,size_frame)
            mp4_name.write(frame)
        elif mp4_name!='':
            mp4_name.write(frame)
        this_time=datetime.now()
        res_time=this_time-save_time #相差时间
        if int(res_time.seconds)>3:
            mp4_name.release()
            mp4_name=''     #结束录制
            save_time=datetime.now()


        #动态参数传递出去
        self.model_parameters[cam_num-1][0]=save_time
        self.model_parameters[cam_num-1][1]=save_score
        self.model_parameters[cam_num-1][2]=save_state
        self.model_parameters[cam_num-1][3]=alarm_num
        self.model_parameters[cam_num-1][4]=alarmNumberlist
        self.model_parameters[cam_num-1][5]=mp4_name
        self.model_parameters[cam_num-1][6]=save_name_mp4
        return frame




    def fun_type_cap(self,frame,cam_num,frame_ori):  
        electron_fence=self.electron_fences[cam_num-1]
        ffmpeg_type=self.ffmpeg_types[cam_num-1].split('-') #使用;分割
        size_frame=(int(ffmpeg_type[3]), int(ffmpeg_type[4]))
        id_list=self.id_list
        #electron_fence  size_frame
        #1.第一次检测到记录或报警触发录制 2.记录转报警触发推送，数量变多触发记录推送 3.数量为0后10秒结束录制
        text=''
        alarm_num_middle=0  #统计报警触发个数
        alarm_state=0   #0为无触发，1为绝对触发，2为可能触发
        #动态参数传递进来
        model_parameter=self.model_parameters[cam_num-1]
        save_time=model_parameter[0]
        save_score=model_parameter[1]
        save_state=model_parameter[2]
        alarm_num=model_parameter[3]
        alarmNumberlist=model_parameter[4]
        mp4_name=model_parameter[5]
        save_name_mp4=model_parameter[6]

        for i in range(len(self.boxes[cam_num-1])):
            box = self.boxes[cam_num-1][i]
            width = frame.shape[1]
            height = frame.shape[0]
            x1 = int((box[0] - box[2] / 2.0) * width)
            y1 = int((box[1] - box[3] / 2.0) * height)
            x2 = int((box[0] + box[2] / 2.0) * width)
            y2 = int((box[1] + box[3] / 2.0) * height)
            if len(box) >= 7 and self.class_names:
                cls_id = box[6]
                text=self.class_names[cls_id]
                #报警统计
                if text=='none' and box[5]>0.8: #不带安全帽的报警值,设置为报警0.8
                    if electron_fence!='None':#如果不设立电子围栏则不带安全帽直接报警
                        electrons=electron_fence.split('-') #使用;分割
                        electron_middle=0                  #电子围栏中间值
                        for electron in electrons:
                            electron=electron[1:-1].split(';')
                            #在电子围栏内时再判断是否在电子屏蔽内
                            if electron[0]=='1': #电子围栏
                                if int(float(electron[1])*size_frame[0])<int(x1) and int(float(electron[2])*size_frame[0])>int(x2) and int(float(electron[3])*size_frame[1])<int(y1) and int(float(electron[4])*size_frame[1])>int(y2) :  
                                    electron_middle=1
                        for electron in electrons:
                            electron=electron[1:-1].split(';')
                            if electron[0]=='0': #电子屏蔽
                                if int(float(electron[1])*size_frame[0])<int(x1) and int(float(electron[2])*size_frame[0])>int(x2) and int(float(electron[3])*size_frame[1])<int(y1) and int(float(electron[4])*size_frame[1])>int(y2) :  
                                    electron_middle=0
                        if electron_middle==1:
                            alarm_num_middle=alarm_num_middle+1 
                            alarm_state=1
                            frame = cv2ImgAddText(frame, "不带安全帽置信度: "+str(box[5]), 10, 100, (0, 0 , 139), 20)
                    else :
                        frame = cv2ImgAddText(frame, "不带安全帽置信度: "+str(box[5]), 10, 100, (0, 0 , 139), 20)     
                        alarm_num_middle=alarm_num_middle+1 
                        alarm_state=1
                if text=='none' and 0.6<box[5]<=0.8: #不带安全帽的报警值,0.6-0.8只记录不报警
                    if electron_fence!='None':#如果不设立电子围栏则不带安全帽直接记录
                        electrons=electron_fence.split('-') #使用;分割
                        electron_middle=0                  #电子围栏中间值
                        for electron in electrons:
                            electron=electron[1:-1].split(';')
                            #在电子围栏内时再判断是否在电子屏蔽内
                            if electron[0]=='1': #电子围栏
                                if int(float(electron[1])*size_frame[0])<int(x1) and int(float(electron[2])*size_frame[0])>int(x2) and int(float(electron[3])*size_frame[1])<int(y1) and int(float(electron[4])*size_frame[1])>int(y2) :  
                                    electron_middle=1
                        for electron in electrons:
                            electron=electron[1:-1].split(';')
                            if electron[0]=='0': #电子屏蔽
                                if int(float(electron[1])*size_frame[0])<int(x1) and int(float(electron[2])*size_frame[0])>int(x2) and int(float(electron[3])*size_frame[1])<int(y1) and int(float(electron[4])*size_frame[1])>int(y2) :  
                                    electron_middle=0
                        if electron_middle==1:
                            alarm_num_middle=alarm_num_middle+1 #统计报警触发个数  
                            if alarm_state!=1:
                                alarm_state=2 #当已经为1时不变
                            frame = cv2ImgAddText(frame, "不带安全帽置信度: "+str(box[5]), 10, 100, (0, 0 , 139), 20)
                    else :
                        alarm_num_middle=alarm_num_middle+1 #统计报警触发个数
                        if alarm_state!=1:
                            alarm_state=2
        this_time=datetime.now()
        res_time=this_time-save_time #相差时间
        if save_state==1:   #先判断是否录制
            mp4_name.write(frame)
        #情况1.1:绝对触发，未曾录制则更新save_time,save_score，报警,alarm_num
        if alarm_state==1 and save_state==0 :
            save_time=datetime.now()
            save_score=6   #0-3为记录，4-6为报警
            save_state=1
            alarm_num=alarm_num_middle
            alarm_post_state=1 #报警
            mp4_name,save_name_mp4=self.save_mp4_init(save_time,cam_num,size_frame)
            mp4_name.write(frame)
            self.post_alarm_thread(frame,save_time,cam_num,id_list,alarm_post_state,frame_ori)
            alarmNumberlist.append(save_time)
            #开始录制接口
        #情况1.2:绝对触发，已经录制则更新save_time,save_score,如果alarm_num_middle大于alarm_num则报警
        elif alarm_state==1 and save_state==1:
            save_time=datetime.now()
            save_score=6
            if alarm_num_middle>alarm_num:
                alarm_num=alarm_num_middle
                alarm_post_state=1 #报警
                self.post_alarm_thread(frame,save_time,cam_num,id_list,alarm_post_state,frame_ori)
                alarmNumberlist.append(save_time)
        #情况1.3:绝对触发，已经录制,但之前是记录则更新save_time,save_score,如果alarm_num_middle大于alarm_num则报警
        elif alarm_state==1 and save_state==1:
            save_time=datetime.now()
            save_score=6
            alarm_post_state=1 #报警
            self.post_alarm_thread(frame,save_time,cam_num,id_list,alarm_post_state,frame_ori)
            alarmNumberlist.append(save_time)
            if alarm_num_middle>alarm_num:
                alarm_num=alarm_num_middle
        #情况2.1:可能触发,未曾录制则更新save_time,save_score，记录,alarm_num
        elif alarm_state==2 and save_state==0 :
            save_time=datetime.now()
            save_score=3
            save_state=1
            alarm_num=alarm_num_middle
            alarm_post_state=2 #记录
            mp4_name,save_name_mp4=self.save_mp4_init(save_time,cam_num,size_frame)
            mp4_name.write(frame)
            self.post_alarm_thread(frame,save_time,cam_num,id_list,alarm_post_state,frame_ori)
            alarmNumberlist.append(save_time)
        #情况2.2:可能触发,已经录制则更新save_time,save_score,如果alarm_num_middle大于alarm_num则记录
        elif alarm_state==2 and save_state==1:
            save_time=datetime.now()
            save_score=3
            if alarm_num_middle>alarm_num:
                alarm_num=alarm_num_middle
                alarm_post_state=2 #记录
                self.post_alarm_thread(frame,save_time,cam_num,id_list,alarm_post_state,frame_ori)
                alarmNumberlist.append(save_time)
        #情况2.3:可能触发,已经录制则更新save_time,save_score,如果alarm_num_middle大于alarm_num则记录
        elif alarm_state==2 and save_state==1:
            save_time=datetime.now()
            save_score=save_score-1
            if alarm_num_middle>alarm_num:
                alarm_num=alarm_num_middle
                alarm_post_state=2 #记录
                self.post_alarm_thread(frame,save_time,cam_num,id_list,alarm_post_state,frame_ori)
                alarmNumberlist.append(save_time)
        #情况3.1:无触发，res_time超过10秒则更新save_time,save_score,save_score判断是否结束录制
        elif alarm_state==0 and int(res_time.seconds)>0:
            if save_state==0:
                save_time=datetime.now()
            elif save_state==1 and save_score<=0:
                save_time=datetime.now()
                mp4_name.release()
                mp4_name=''     #结束录制
                for alarmNumberlist1 in alarmNumberlist:
                    self.post_stop_mp4(alarmNumberlist1,id_list,cam_num,save_name_mp4)
                alarmNumberlist=[]
                save_score=0
                save_state=0
            elif save_state==1 and save_score>0 :
                save_time=datetime.now()
                save_score=save_score-1

        #动态参数传递出去

        model_parameter=[save_time,save_score,save_state,alarm_num,alarmNumberlist,mp4_name,save_name_mp4]
        self.model_parameters[cam_num-1]=model_parameter
        return frame


    def fun_type_gang(self,frame,cam_num,frame_ori):  
        electron_fence=self.electron_fences[cam_num-1]
        ffmpeg_type=self.ffmpeg_types[cam_num-1].split('-') #使用;分割
        size_frame=(int(ffmpeg_type[3]), int(ffmpeg_type[4]))
        id_list=self.id_list

        text=''
        gang_door_state=0  #是否出现钢或门的标置位
        alarm_state=0

        #动态参数传递进来
        model_parameter=self.model_parameters[cam_num-1]
        save_time=model_parameter[0]
        save_score=model_parameter[1]
        save_state=model_parameter[2]
        alarm_num=model_parameter[3]
        alarmNumberlist=model_parameter[4]
        mp4_name=model_parameter[5]
        save_name_mp4=model_parameter[6]
        if False:  #用于进行现场截图
            if str(id_list[cam_num-1])=='7' or str(id_list[cam_num-1])=='8' or str(id_list[cam_num-1])=='9':
                if len(self.boxes[cam_num-1])>0:
                    date_time=datetime.now()
                    save_name=str(date_time.date())+'-'+str(date_time.hour)+'-'+str(date_time.minute)+'-'+str(date_time.second)+'.jpg'
                    dirs = 'save_picture/ori/'+str(id_list[cam_num-1])
                    if not os.path.exists(dirs):
                        os.makedirs(dirs)
                    dirs_res = 'save_picture/res/'+str(id_list[cam_num-1])
                    if not os.path.exists(dirs_res):
                        os.makedirs(dirs_res)
                    cv2.imwrite(dirs+'/'+save_name,frame_ori)
                    cv2.imwrite(dirs_res+'/'+save_name,frame)
        for i in range(len(self.boxes[cam_num-1])):
            box = self.boxes[cam_num-1][i]
            width = frame.shape[1]
            height = frame.shape[0]
            x1 = int((box[0] - box[2] / 2.0) * width)
            y1 = int((box[1] - box[3] / 2.0) * height)
            x2 = int((box[0] + box[2] / 2.0) * width)
            y2 = int((box[1] + box[3] / 2.0) * height)
            if len(box) >= 7 and self.class_names:
                cls_id = box[6]
                text=self.class_names[cls_id]
                if False:  #用于进行现场截图
                    if str(id_list[cam_num-1])=='7' or str(id_list[cam_num-1])=='8' or str(id_list[cam_num-1])=='9':
                        date_time=datetime.now()
                        save_name=str(date_time.date())+'-'+str(date_time.hour)+'-'+str(date_time.minute)+'-'+str(date_time.second)+'.jpg'
                        dirs = 'save_picture/ori/'+str(id_list[cam_num-1])
                        if not os.path.exists(dirs):
                            os.makedirs(dirs)
                        dirs_res = 'save_picture/res/'+str(id_list[cam_num-1])
                        if not os.path.exists(dirs_res):
                            os.makedirs(dirs_res)
                        cv2.imwrite(dirs+'/'+save_name,frame_ori)
                        cv2.imwrite(dirs_res+'/'+save_name,frame)

           #     if text=='gang' or text=='door':  #判断是否打开炉门或者进出钢
                if text=='door':  #判断是否打开炉门
                    gang_door_state=1   
                if text=='fire': #检测到火焰，则判断火焰大小来报警
                    if (x2-x1)*(y2-y1)>50000:
                        frame = cv2ImgAddText(frame, "火焰面积: "+str((x2-x1)*(y2-y1)), 10, 100, (0, 0 , 139), 20)
                       # save_score=11   #火焰过大是立即报警  #这里封了
                       # alarm_state=1 
        this_time=datetime.now()
        res_time=this_time-save_time #相差时间
        if gang_door_state==0:
            for i in range(len(self.boxes[cam_num-1])):
                box = self.boxes[cam_num-1][i]
                width = frame.shape[1]
                height = frame.shape[0]
                x1 = int((box[0] - box[2] / 2.0) * width)
                y1 = int((box[1] - box[3] / 2.0) * height)
                x2 = int((box[0] + box[2] / 2.0) * width)
                y2 = int((box[1] + box[3] / 2.0) * height)
                if len(box) >= 7 and self.class_names:
                    cls_id = box[6]
                    text=self.class_names[cls_id]
                    if text=='firehua':
                        if electron_fence!='None':#如果不设立电子围栏则直接报警
                            electrons=electron_fence.split('-') #使用;分割
                            electron_middle=0                  #电子围栏中间值
                            for electron in electrons:
                                electron=electron[1:-1].split(';')
                                #在电子围栏内时再判断是否在电子屏蔽内
                                if electron[0]=='1': #电子围栏
                                    if int(float(electron[1])*size_frame[0])<int(x1) and int(float(electron[2])*size_frame[0])>int(x2) and int(float(electron[3])*size_frame[1])<int(y1) and int(float(electron[4])*size_frame[1])>int(y2) :  
                                        electron_middle=1
                            for electron in electrons:
                                electron=electron[1:-1].split(';')
                                if electron[0]=='0': #电子屏蔽
                                    if int(float(electron[1])*size_frame[0])<int(x1) and int(float(electron[2])*size_frame[0])>int(x2) and int(float(electron[3])*size_frame[1])<int(y1) and int(float(electron[4])*size_frame[1])>int(y2) :  
                                        electron_middle=0
                            if electron_middle==1:
                                alarm_state=1
                        else :
                            alarm_state=1
        if save_state==1:   #先判断是否录制
            mp4_name.write(frame)
        #时间过去1秒且触发再进行判断:
        if int(res_time.seconds)>0 and alarm_state==1:
            #情况1.1:时间过去1秒:
            if 0<int(res_time.seconds)<=2: 
                save_time=datetime.now()
                save_score=save_score+2
            #情况1.2:时间过去2-4秒:
            elif 2<int(res_time.seconds)<=5: 
                save_time=datetime.now()
                save_score=save_score+1
            #情况1.3:时间过去5秒以上:
            else : 
                save_time=datetime.now()
            #情况2.1:save_score>10,未录制,报警+录制
            if save_score>10 and save_state==0 : 
                save_state=1  
                save_score=11
                alarm_post_state=1 #报警

                print_txt=str(save_time)
                write_txt(id_list,cam_num,print_txt)
                print_txt=str(alarmNumberlist)
                write_txt(id_list,cam_num,print_txt)
                #开始录制
                mp4_name,save_name_mp4=self.save_mp4_init(save_time,cam_num,size_frame)
                mp4_name.write(frame)

                self.post_alarm_thread(frame,save_time,cam_num,id_list,alarm_post_state,frame_ori)
                alarmNumberlist.append(save_time)

                print_txt=str(alarmNumberlist)
                write_txt(id_list,cam_num,print_txt)

            #情况2.2:save_score>10,已录制
            if save_score>10 and save_state==1 : 
                save_score=11
        #时间过去10秒且一直无触发:    
        elif int(res_time.seconds)>=10 and alarm_state==0:
            #情况2.1:无触发，正在录制,停止录制
            if save_state==1 :
                save_time=datetime.now()
                save_score=0
                save_state=0
                mp4_name.release()

                print_txt=str(save_time)
                write_txt(id_list,cam_num,print_txt)
                print_txt=str(alarmNumberlist)
                write_txt(id_list,cam_num,print_txt)


                for alarmNumberlist1 in alarmNumberlist:
                    self.post_stop_mp4(alarmNumberlist1,id_list,cam_num,save_name_mp4)
                alarmNumberlist=[]
                mp4_name=''     #结束录制
            #情况2.2:无触发，未录制,清0
            elif save_state==0 :
                save_time=datetime.now()
                save_score=0
        if save_state==1:
            frame = cv2ImgAddText(frame, "炉口火苗报警", 10, 100, (0, 0 , 139), 20)

        #动态参数传递出去
        model_parameter=[save_time,save_score,save_state,alarm_num,alarmNumberlist,mp4_name,save_name_mp4]
        self.model_parameters[cam_num-1]=model_parameter
        return frame

    def fun_type_electric(self,frame,cam_num,frame_ori):  
        electron_fence=self.electron_fences[cam_num-1]
        ffmpeg_type=self.ffmpeg_types[cam_num-1].split('-') #使用;分割
        size_frame=(int(ffmpeg_type[3]), int(ffmpeg_type[4]))
        id_list=self.id_list
        text=''
        alarm_state=0
        #动态参数传递进来
        model_parameter=self.model_parameters[cam_num-1]
        save_time=model_parameter[0]
        save_score=model_parameter[1]
        save_state=model_parameter[2]
        alarm_num=model_parameter[3]
        alarmNumberlist=model_parameter[4]
        mp4_name=model_parameter[5]
        save_name_mp4=model_parameter[6]

        for i in range(len(self.boxes[cam_num-1])):
            box = self.boxes[cam_num-1][i]
            width = frame.shape[1]
            height = frame.shape[0]
            x1 = int((box[0] - box[2] / 2.0) * width)
            y1 = int((box[1] - box[3] / 2.0) * height)
            x2 = int((box[0] + box[2] / 2.0) * width)
            y2 = int((box[1] + box[3] / 2.0) * height)
            if len(box) >= 7 and self.class_names:
                cls_id = box[6]
                text=self.class_names[cls_id]
                if box[5]>0.8: 
                    #只要识别到就判断是否在电子围栏
                    if electron_fence!='None':#如果不设立电子围栏则直接报警
                        electrons=electron_fence.split('-') #使用;分割
                        electron_middle=0                  #电子围栏中间值
                        for electron in electrons:
                            electron=electron[1:-1].split(';')
                            #在电子围栏内时再判断是否在电子屏蔽内
                            if electron[0]=='1': #电子围栏
                                if int(float(electron[1])*size_frame[0])<int(x1) and int(float(electron[2])*size_frame[0])>int(x2) and int(float(electron[3])*size_frame[1])<int(y1) and int(float(electron[4])*size_frame[1])>int(y2) :  
                                    electron_middle=1
                        for electron in electrons:
                            electron=electron[1:-1].split(';')
                            if electron[0]=='0': #电子屏蔽
                                if int(float(electron[1])*size_frame[0])<int(x1) and int(float(electron[2])*size_frame[0])>int(x2) and int(float(electron[3])*size_frame[1])<int(y1) and int(float(electron[4])*size_frame[1])>int(y2) :  
                                    electron_middle=0
                        if electron_middle==1:
                            alarm_state=1
                    else :
                        alarm_state=1
        this_time=datetime.now()
        res_time=this_time-save_time #相差时间
        if save_state==1:   #先判断是否录制
            mp4_name.write(frame)
        #时间过去1秒以上且触发再进行判断:
        if int(res_time.seconds)>0 and alarm_state==1:
            save_time=datetime.now()
            #情况1.1:之前未录制则报警+录制:
            if save_state==0 : 
                save_state=1
                save_score=6
                alarm_post_state=1 #报警
                mp4_name,save_name_mp4=self.save_mp4_init(save_time,cam_num,size_frame)
                mp4_name.write(frame)
                self.post_alarm_thread(frame,save_time,cam_num,id_list,alarm_post_state,frame_ori)
                alarmNumberlist.append(save_time)
            #情况1.1:之前已录制只置6:
            if save_state==1 : 
                save_score=6
        #时间过去5秒且无触发:
        elif int(res_time.seconds)>4 and alarm_state==0:
            #情况2.1:无触发，正在录制,停止录制
            if save_state==1 :
                save_time=datetime.now()
                save_score=0
                save_state=0
                mp4_name.release()
                for alarmNumberlist1 in alarmNumberlist:
                    self.post_stop_mp4(alarmNumberlist1,id_list,cam_num,save_name_mp4)
                alarmNumberlist=[]
                mp4_name=''     #结束录制
            #情况2.2:无触发，未录制,清0
            elif save_state==0 :
                save_time=datetime.now()
                save_score=0

        #动态参数传递出去
        model_parameter=[save_time,save_score,save_state,alarm_num,alarmNumberlist,mp4_name,save_name_mp4]
        self.model_parameters[cam_num-1]=model_parameter
        return frame

    def fun_type_zhuanggang(self,frame,cam_num,frame_ori):  
        electron_fence=self.electron_fences[cam_num-1]
        ffmpeg_type=self.ffmpeg_types[cam_num-1].split('-') #使用;分割
        size_frame=(int(ffmpeg_type[3]), int(ffmpeg_type[4]))
        id_list=self.id_list

        text=''
        alarm_state=0
        #动态参数传递进来
        model_parameter=self.model_parameters[cam_num-1]
        save_time=model_parameter[0]
        save_score=model_parameter[1]
        save_state=model_parameter[2]
        alarm_num=model_parameter[3]
        alarmNumberlist=model_parameter[4]
        mp4_name=model_parameter[5]
        save_name_mp4=model_parameter[6]

        for i in range(len(self.boxes[cam_num-1])):
            box = self.boxes[cam_num-1][i]
            width = frame.shape[1]
            height = frame.shape[0]
            x1 = int((box[0] - box[2] / 2.0) * width)
            y1 = int((box[1] - box[3] / 2.0) * height)
            x2 = int((box[0] + box[2] / 2.0) * width)
            y2 = int((box[1] + box[3] / 2.0) * height)
            if len(box) >= 7 and self.class_names:
                cls_id = box[6]
                text=self.class_names[cls_id]
                if box[5]>0.9:   #判断火焰识别分数超过0.9再报警，减少误报频率
                    #只要识别到就判断是否在电子围栏
                    if electron_fence!='None':#如果不设立电子围栏则直接报警
                        electrons=electron_fence.split('-') #使用;分割
                        electron_middle=0                  #电子围栏中间值
                        for electron in electrons:
                            electron=electron[1:-1].split(';')
                            #在电子围栏内时再判断是否在电子屏蔽内
                            if electron[0]=='1': #电子围栏
                                if int(float(electron[1])*size_frame[0])<int(x1) and int(float(electron[2])*size_frame[0])>int(x2) and int(float(electron[3])*size_frame[1])<int(y1) and int(float(electron[4])*size_frame[1])>int(y2) :  
                                    electron_middle=1
                        for electron in electrons:
                            electron=electron[1:-1].split(';')
                            if electron[0]=='0': #电子屏蔽
                                if int(float(electron[1])*size_frame[0])<int(x1) and int(float(electron[2])*size_frame[0])>int(x2) and int(float(electron[3])*size_frame[1])<int(y1) and int(float(electron[4])*size_frame[1])>int(y2) :  
                                    electron_middle=0
                        if electron_middle==1:
                            alarm_state=1
                    else :
                        alarm_state=1
        this_time=datetime.now()
        res_time=this_time-save_time #相差时间
        if save_state==1:   #先判断是否录制
            mp4_name.write(frame)
        #时间过去1秒以上且触发再进行判断:
        if int(res_time.seconds)>0 and alarm_state==1:
            #情况1.1:之前未录制则报警+录制:
            if save_state==0 : 
                save_state=1
                save_score=11
                alarm_post_state=1 #报警
                mp4_name,save_name_mp4=self.save_mp4_init(save_time,cam_num,size_frame)
                mp4_name.write(frame)
                self.post_alarm_thread(frame,save_time,cam_num,id_list,alarm_post_state,frame_ori)
                alarmNumberlist.append(save_time)
                if True:  #用于进行现场截图
                    date_time=datetime.now()
                    save_name=str(date_time.date())+'-'+str(date_time.hour)+'-'+str(date_time.minute)+'-'+str(date_time.second)+'.jpg'
                    dirs = 'save_picture/ori/'+str(id_list[cam_num-1])
                    if not os.path.exists(dirs):
                        os.makedirs(dirs)
                    dirs_res = 'save_picture/res/'+str(id_list[cam_num-1])
                    if not os.path.exists(dirs_res):
                        os.makedirs(dirs_res)
                    cv2.imwrite(dirs+'/'+save_name,frame_ori)
                    cv2.imwrite(dirs_res+'/'+save_name,frame)
            #情况1.1:之前已录制只置11:
            if save_state==1 : 
                save_score=11
        #时间过去5秒且无触发:
        elif int(res_time.seconds)>4 and alarm_state==0:
            #情况2.1:无触发，正在录制,停止录制
            if save_state==1 :
                save_score=0
                save_state=0
                mp4_name.release()
                for alarmNumberlist1 in alarmNumberlist:
                    self.post_stop_mp4(alarmNumberlist1,id_list,cam_num,save_name_mp4)
                alarmNumberlist=[]
                mp4_name=''     #结束录制
            #情况2.2:无触发，未录制,清0
            elif save_state==0 :
                save_score=0
        #动态参数传递出去
        model_parameter=[save_time,save_score,save_state,alarm_num,alarmNumberlist,mp4_name,save_name_mp4]
        self.model_parameters[cam_num-1]=model_parameter
        return frame


    def fun_type_fire(self,frame,cam_num,frame_ori):  
        electron_fence=self.electron_fences[cam_num-1]
        ffmpeg_type=self.ffmpeg_types[cam_num-1].split('-') #使用;分割
        size_frame=(int(ffmpeg_type[3]), int(ffmpeg_type[4]))
        id_list=self.id_list

        text=''
        alarm_state=0
        #动态参数传递进来
        model_parameter=self.model_parameters[cam_num-1]
        save_time=model_parameter[0]
        save_score=model_parameter[1]
        save_state=model_parameter[2]
        alarm_num=model_parameter[3]
        alarmNumberlist=model_parameter[4]
        mp4_name=model_parameter[5]
        save_name_mp4=model_parameter[6]

        for i in range(len(self.boxes[cam_num-1])):
            box = self.boxes[cam_num-1][i]
            width = frame.shape[1]
            height = frame.shape[0]
            x1 = int((box[0] - box[2] / 2.0) * width)
            y1 = int((box[1] - box[3] / 2.0) * height)
            x2 = int((box[0] + box[2] / 2.0) * width)
            y2 = int((box[1] + box[3] / 2.0) * height)
            if len(box) >= 7 and self.class_names:
                cls_id = box[6]
                text=self.class_names[cls_id]
                if box[5]>0.9:   #判断火焰识别分数超过0.9再报警，减少误报频率
                    #只要识别到就判断是否在电子围栏
                    if electron_fence!='None':#如果不设立电子围栏则直接报警
                        electrons=electron_fence.split('-') #使用;分割
                        electron_middle=0                  #电子围栏中间值
                        for electron in electrons:
                            electron=electron[1:-1].split(';')
                            #在电子围栏内时再判断是否在电子屏蔽内
                            if electron[0]=='1': #电子围栏
                                if int(float(electron[1])*size_frame[0])<int(x1) and int(float(electron[2])*size_frame[0])>int(x2) and int(float(electron[3])*size_frame[1])<int(y1) and int(float(electron[4])*size_frame[1])>int(y2) :  
                                    electron_middle=1
                        for electron in electrons:
                            electron=electron[1:-1].split(';')
                            if electron[0]=='0': #电子屏蔽
                                if int(float(electron[1])*size_frame[0])<int(x1) and int(float(electron[2])*size_frame[0])>int(x2) and int(float(electron[3])*size_frame[1])<int(y1) and int(float(electron[4])*size_frame[1])>int(y2) :  
                                    electron_middle=0
                        if electron_middle==1:
                            alarm_state=1
                    else :
                        alarm_state=1
        this_time=datetime.now()
        res_time=this_time-save_time #相差时间
       # if save_state==1:   #先判断是否录制
       #     mp4_name.write(frame)
        #时间过去1秒以上且触发再进行判断:
        if int(res_time.seconds)>0 and alarm_state==1:
            #情况1.1:之前未录制则报警+录制:
            if save_state==0 : 
                save_state=1
                save_score=11
                alarm_post_state=1 #报警
            #    mp4_name,save_name_mp4=self.save_mp4_init(save_time,cam_num,size_frame)
            #    mp4_name.write(frame)
            #    self.post_alarm_thread(frame,save_time,cam_num,id_list,alarm_post_state,frame_ori)
            #    alarmNumberlist.append(save_time)
                if True:  #用于进行现场截图
                    date_time=datetime.now()
                    save_name=str(date_time.date())+'-'+str(date_time.hour)+'-'+str(date_time.minute)+'-'+str(date_time.second)+'.jpg'
                    dirs = 'save_picture/ori/'+str(id_list[cam_num-1])
                    if not os.path.exists(dirs):
                        os.makedirs(dirs)
                    dirs_res = 'save_picture/res/'+str(id_list[cam_num-1])
                    if not os.path.exists(dirs_res):
                        os.makedirs(dirs_res)
                    cv2.imwrite(dirs+'/'+save_name,frame_ori)
                    cv2.imwrite(dirs_res+'/'+save_name,frame)
            #情况1.1:之前已录制只置11:
            if save_state==1 : 
                save_score=11
        #时间过去5秒且无触发:
        elif int(res_time.seconds)>4 and alarm_state==0:
            #情况2.1:无触发，正在录制,停止录制
            if save_state==1 :
                save_score=0
                save_state=0
            #    mp4_name.release()
            #    for alarmNumberlist1 in alarmNumberlist:
            #        self.post_stop_mp4(alarmNumberlist1,id_list,cam_num,save_name_mp4)
            #    alarmNumberlist=[]
            #    mp4_name=''     #结束录制
            #情况2.2:无触发，未录制,清0
            elif save_state==0 :
                save_score=0
        #动态参数传递出去
        model_parameter=[save_time,save_score,save_state,alarm_num,alarmNumberlist,mp4_name,save_name_mp4]
        self.model_parameters[cam_num-1]=model_parameter
        return frame


    def post_alarm_thread(self,frame,save_time,cam_num,id_list,alarm_post_state,frame_ori):
        post_thread = Thread_post(id_list[cam_num-1],save_time,frame,self.model_fun,alarm_post_state,frame_ori)
        post_thread.start()
    def post_stop_mp4(self,save_time,id_list,cam_num,mp4_name):
        alarmNumber=str(save_time)+str(id_list[cam_num-1])
        ffmpeg_post_thread = Thread_ffmpeg_post(mp4_name,alarmNumber,str(id_list[cam_num-1]))
        ffmpeg_post_thread.start()

    def save_mp4_init(self,save_time,cam_num,size_frame):
        dirs_res = 'save_mp4/'+str(self.id_list[cam_num-1])
        if not os.path.exists(dirs_res):
            os.makedirs(dirs_res)
        save_name=dirs_res+'/'+str(save_time.date())+'-'+str(save_time.hour)+'-'+str(save_time.minute)+'-'+str(save_time.second)+'.avi'
        mp4_name = cv2.VideoWriter(save_name, cv2.VideoWriter_fourcc('X','V','I','D'), 10, (size_frame[0], size_frame[1]))
        return mp4_name,save_name







